﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UserControlLib
{
    /// <summary>
    /// RainbowText.xaml 的交互逻辑
    /// </summary>
    /// <summary>
    /// RainbowText.xaml 的交互逻辑
    /// </summary>
    public partial class RainbowText : UserControl
    {
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(RainbowText), new PropertyMetadata(null, OnTextChanged));

        public string Text
        {
            get => (string)GetValue(TextProperty);
            set => SetValue(TextProperty, value);
        }

        private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as RainbowText;
            if (control != null)
            {
                control.UpdateCharacterCollection();
            }
        }

        public static readonly DependencyProperty CharactersProperty =
            DependencyProperty.Register("Characters", typeof(ObservableCollection<string>), typeof(RainbowText), new PropertyMetadata(null));

        public ObservableCollection<string> Characters
        {
            get => (ObservableCollection<string>)GetValue(CharactersProperty);
            set => SetValue(CharactersProperty, value);
        }

        // 定义 Orientation 依赖属性
        public static readonly DependencyProperty OrientationProperty =
            DependencyProperty.Register("Orientation", typeof(Orientation), typeof(RainbowText),
                new PropertyMetadata(Orientation.Horizontal, OnOrientationChanged));

        public Orientation Orientation
        {
            get => (Orientation)GetValue(OrientationProperty);
            set => SetValue(OrientationProperty, value);
        }

        private static void OnOrientationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as RainbowText;
            if (control != null)
            {
                control.UpdateItemsPanel();
            }
        }

        private void UpdateItemsPanel()
        {
            // 使用 Style 和 Triggers 动态设置 ItemsPanel
            var style = this.Resources["ItemsControlStyle"] as Style;
            if (style != null)
            {
                PART_ItemsControl.Style = style;
            }
        }

        public RainbowText()
        {
            InitializeComponent();
            UpdateCharacterCollection();
            Loaded += (sender, e) => UpdateItemsPanel(); // 确保在控件加载完成后更新面板
        }

        private void UpdateCharacterCollection()
        {
            var newCharacters = new ObservableCollection<string>();
            if (!string.IsNullOrEmpty(Text))
            {
                foreach (var ch in Text)
                {
                    newCharacters.Add(ch.ToString());
                }
            }

            // 更新依赖属性
            Characters = newCharacters;
        }
    }

}
